package com.hsmw.api.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwForcemanEvaluateMapper;
import com.hsmw.api.mapper.HsmwForcemanMedalMapper;
import com.hsmw.api.mapper.HsmwItemConcreteRelationMapper;
import com.hsmw.api.mapper.HsmwScoringRecordMapper;
import com.hsmw.api.vo.DriverInfoVO;
import com.hsmw.api.vo.EvaluateInfoVO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.constant.HtnUserTypeEnum;
import com.htn.common.core.constant.OperTypeEnum;
import com.htn.common.core.constant.PumpOrderStatusEnum;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwForcemanEvaluateService extends BaseService<HsmwForcemanEvaluateMapper, HsmwForcemanEvaluate> {


    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwForcemanMedalMapper hsmwForcemanMedalMapper;

    @Autowired
    private HsmwUserDriverService  hsmwUserDriverService;

    @Autowired
    private HsmwPumpOperateRecordService  hsmwPumpOperateRecordService;

    @Autowired
    private HsmwScoringRecordMapper hsmwScoringRecordMapper;

    @Autowired
    private HsmwOrderVehicleRelationService hsmwOrderVehicleRelationService;

    @Autowired
    private HsmwHtnUserService hsmwHtnUserService;

    public DataResponse saveOrUpdateEvaluate(EvaluateInfoVO vo) {

        List<HsmwForcemanEvaluate> hsmwForcemanEvaluates = vo.getHsmwForcemanEvaluates();
        LoginUser hsmwUser = currentHsmwUserUtils.getCurrentUserInfo();
        for (HsmwForcemanEvaluate hsmwForcemanEvaluate : hsmwForcemanEvaluates) {
            if(StringUtils.isBlank(hsmwForcemanEvaluate.getSatisfied())){
                continue;
            }
            if(StringUtils.isNotBlank(hsmwForcemanEvaluate.getId())){

                boolean b = this.updateById(hsmwForcemanEvaluate);

            }else {
                HsmwPumpOperateRecord record = new HsmwPumpOperateRecord();
                record.setOrderId(hsmwForcemanEvaluate.getOrderId());
                record.setUsername(hsmwUser.getUsername());
                record.setOrderStatus(PumpOrderStatusEnum.EVALUATE.getValue());
                record.setCreateTime(LocalDateTime.now());
                DriverInfoVO driver =hsmwUserDriverService.getBizDriverById(hsmwForcemanEvaluate.getDriverId());
                String evaluate= CommonConstants.YES.equals(hsmwForcemanEvaluate.getSatisfied()) ? "好评\uD83D\uDE0A":"差评\uD83D\uDE13";
                record.setOperContent("给司机"+driver.getName() +" "+driver.getUsername()+evaluate);
                hsmwPumpOperateRecordService.save(record);
                hsmwForcemanEvaluate.setFormanId(hsmwUser.getId());
                boolean save = this.save(hsmwForcemanEvaluate);
                HsmwScoringRecord scoringRecord = new HsmwScoringRecord();
                String driverId = hsmwForcemanEvaluate.getDriverId();
                HsmwUserDriver userDriver = hsmwUserDriverService.getById(driverId);
                if(hsmwUser != null && StringUtils.equals("HTN",hsmwUser.getType())){
                    // 当前登录人 类型 为工长 (m.FOREMAN.getValue().equals(hsmwUser.getType()) || HtnUserTypeEnum.COMPANY.getValue().equals(hsmwUser.getType()) || HtnUserTypeEnum.PLATFORM.getValue().equals(hsmwUser.getType()))
                    HsmwForcemanMedal hsmwForcemanMedal = hsmwForcemanMedalMapper.selectOne(new LambdaQueryWrapper<HsmwForcemanMedal>().eq(HsmwForcemanMedal::getUserId,hsmwUser.getId()));
                    if(hsmwForcemanMedal == null){
                        HsmwForcemanMedal forcemanMedal = new HsmwForcemanMedal();
                        forcemanMedal.setCommentCount(1L);
                        forcemanMedal.setUserId(hsmwUser.getId());
                        if("1".equals(hsmwForcemanEvaluate.getSatisfied())){
                            forcemanMedal.setPraiseCount(1L);
                            scoringRecord.setDriverId(userDriver.getId());
                            scoringRecord.setInitialScore(userDriver.getScore());
                            scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) +3 ));
                            scoringRecord.setFluctuateScore("3");
                            scoringRecord.setRemark("订单好评");
                            hsmwScoringRecordMapper.insert(scoringRecord);
                            userDriver.setScore(String.valueOf(Long.parseLong(driver.getScore()) +3 ));
                            hsmwUserDriverService.updateById(userDriver);
                        }else {
                            forcemanMedal.setNegativeCount(1L);
                            scoringRecord.setDriverId(userDriver.getId());
                            scoringRecord.setInitialScore(userDriver.getScore());
                            scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) - 5));
                            scoringRecord.setFluctuateScore("-5");
                            scoringRecord.setRemark("获得差评");
                            hsmwScoringRecordMapper.insert(scoringRecord);
                            userDriver.setScore(String.valueOf(Long.parseLong(userDriver.getScore()) -5 ));
                            hsmwUserDriverService.updateById(userDriver);
                        }
                        hsmwForcemanMedalMapper.insert(forcemanMedal);
                    }else {
                        if("1".equals(hsmwForcemanEvaluate.getSatisfied())){
                            hsmwForcemanMedal.setPraiseCount(hsmwForcemanMedal.getPraiseCount() +1);
                            scoringRecord.setDriverId(userDriver.getId());
                            scoringRecord.setInitialScore(userDriver.getScore());
                            scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) +3 ));
                            scoringRecord.setFluctuateScore("3");
                            scoringRecord.setRemark("订单好评");
                            hsmwScoringRecordMapper.insert(scoringRecord);
                            userDriver.setScore(String.valueOf(Long.parseLong(userDriver.getScore()) +3 ));
                            hsmwUserDriverService.updateById(userDriver);
                        }else {
                            hsmwForcemanMedal.setNegativeCount(hsmwForcemanMedal.getNegativeCount() +1L);
                            scoringRecord.setDriverId(userDriver.getId());
                            scoringRecord.setInitialScore(userDriver.getScore());
                            scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) - 5));
                            scoringRecord.setFluctuateScore("-5");
                            scoringRecord.setRemark("获得差评");
                            hsmwScoringRecordMapper.insert(scoringRecord);
                            userDriver.setScore(String.valueOf(Long.parseLong(userDriver.getScore()) -5 ));
                            hsmwUserDriverService.updateById(userDriver);
                        }
                        hsmwForcemanMedal.setCommentCount(hsmwForcemanMedal.getCommentCount() + 1L);
                        hsmwForcemanMedalMapper.updateById(hsmwForcemanMedal);
                    }
                }
            }
        }
        if(StringUtils.isNotBlank(vo.getRelationId())){
            HsmwOrderVehicleRelation relation = hsmwOrderVehicleRelationService.getById(vo.getRelationId());
            relation.setStarRating(vo.getStarRating());
            hsmwOrderVehicleRelationService.updateById(relation);
        }

        return new DataResponse(CommonConstants.ResponseStatus.SUCCESS);
    }
}
